home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Taiji Applet Pack v2.7 / TicTacToe / TicTacToe.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-11-06  |  12.1 KB  |  560 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Button;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Container;
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9. import java.awt.Graphics;
  10. import java.awt.GridLayout;
  11. import java.awt.Image;
  12. import java.awt.Label;
  13. import java.awt.MediaTracker;
  14. import java.awt.Panel;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import java.awt.event.InputEvent;
  18. import java.awt.event.MouseEvent;
  19. import java.awt.event.MouseListener;
  20. import java.net.URL;
  21. import java.util.EventObject;
  22.  
  23. public class TicTacToe extends Applet implements MouseListener, ActionListener {
  24.    // $FF: renamed from: wi int
  25.    int field_0;
  26.    // $FF: renamed from: he int
  27.    int field_1;
  28.    Color backColor;
  29.    final Color borderColorDef;
  30.    Color borderColor;
  31.    final int nCases;
  32.    Image buffer;
  33.    // $FF: renamed from: b java.awt.Graphics
  34.    Graphics field_2;
  35.    Image crossIm;
  36.    Image circleIm;
  37.    int[][] mat;
  38.    int cote;
  39.    boolean loaded;
  40.    int nCoups;
  41.    int first;
  42.    int mode;
  43.    boolean resetable;
  44.    boolean textable;
  45.    String text;
  46.    // $FF: renamed from: p java.awt.Panel
  47.    Panel field_3;
  48.    boolean playable;
  49.    final String memInit;
  50.    private String mem;
  51.    private final int[][][] rep;
  52.    private AudioClip clicSound;
  53.    private AudioClip computerSound;
  54.    private AudioClip playerSound;
  55.    private AudioClip drawSound;
  56.  
  57.    public String getAppletInfo() {
  58.       return "Name: TicTacToe\r\nAuthor: Taiji Software\r\n";
  59.    }
  60.  
  61.    public TicTacToe() {
  62.       this.backColor = Color.white;
  63.       this.borderColorDef = Color.black;
  64.       this.nCases = 3;
  65.       this.memInit = "000 000 000 000 000 000 000 000";
  66.       this.mem = "000 000 000 000 000 000 000 000";
  67.       this.rep = new int[][][]{{{0, 12, 24}, {1, 16}, {2, 20, 30}}, {{4, 13}, {5, 17, 25, 29}, {6, 21}}, {{8, 14, 28}, {9, 18}, {10, 22, 26}}};
  68.       this.mode = 1;
  69.       this.first = 0;
  70.       this.borderColor = this.borderColorDef;
  71.       ((Component)this).addMouseListener(this);
  72.    }
  73.  
  74.    public void register() {
  75.       try {
  76.          URL u = new URL("http://www.taijisoftware.com");
  77.          ((Applet)this).getAppletContext().showDocument(u, "_blank");
  78.          ((Applet)this).stop();
  79.       } catch (Exception e) {
  80.          System.out.println(e);
  81.          ((Applet)this).stop();
  82.       }
  83.    }
  84.  
  85.    public void init() {
  86.       String codeBase = null;
  87.  
  88.       try {
  89.          codeBase = (new URL(((Applet)this).getCodeBase().toString())).getHost();
  90.          System.out.println("Copyright 1999, 2001 Taiji Software(tm)\nYour domain name is : " + codeBase);
  91.          codeBase = codeBase.toUpperCase();
  92.       } catch (Exception var10) {
  93.       }
  94.  
  95.       if (!((Applet)this).getCodeBase().toString().toUpperCase().startsWith("FILE") || ((Applet)this).getParameter("debug") != null) {
  96.          String regCode = ((Applet)this).getParameter("registration_code");
  97.          if (regCode == null) {
  98.             regCode = ((Applet)this).getParameter("reg_domain");
  99.             if (regCode == null) {
  100.                this.register();
  101.             } else {
  102.                if (regCode.length() == codeBase.length() + 4 && !codeBase.startsWith("WWW.")) {
  103.                   codeBase = "WWW." + codeBase;
  104.                } else if (regCode.length() == codeBase.length() - 4 && codeBase.startsWith("WWW.")) {
  105.                   codeBase = codeBase.substring(4);
  106.                }
  107.  
  108.                char[] chars = new char[codeBase.length()];
  109.                codeBase.getChars(0, codeBase.length(), chars, 0);
  110.                String key = new String("haricot");
  111.                char[] chars2 = new char[key.length()];
  112.                key.getChars(0, key.length(), chars2, 0);
  113.  
  114.                for(int i = 0; i < codeBase.length(); ++i) {
  115.                   int j;
  116.                   if (i >= key.length()) {
  117.                      j = i - key.length() * (i / key.length());
  118.                   } else {
  119.                      j = i;
  120.                   }
  121.  
  122.                   chars[i] += chars2[j];
  123.                   chars[i] = (char)(chars[i] - chars[i] / 26 * 26 + 97);
  124.                }
  125.  
  126.                String res = new String(chars);
  127.                if (!res.equalsIgnoreCase(regCode)) {
  128.                   this.register();
  129.                }
  130.             }
  131.          } else if (!regCode.equals("settevercsedegnamiaj") && !regCode.equals("8078")) {
  132.             this.register();
  133.          }
  134.       }
  135.  
  136.       this.getParameters();
  137.       this.crossIm = ((Applet)this).getImage(((Applet)this).getCodeBase(), ((Applet)this).getParameter("cross"));
  138.       this.circleIm = ((Applet)this).getImage(((Applet)this).getCodeBase(), ((Applet)this).getParameter("circle"));
  139.       MediaTracker tracker = new MediaTracker(this);
  140.       tracker.addImage(this.crossIm, 0);
  141.       tracker.addImage(this.circleIm, 0);
  142.  
  143.       try {
  144.          tracker.waitForAll();
  145.          this.loaded = !tracker.isErrorAny();
  146.       } catch (Exception e) {
  147.          System.err.println(e);
  148.       }
  149.  
  150.       if (!this.loaded) {
  151.          ((Applet)this).stop();
  152.       }
  153.  
  154.       this.cote = (this.field_1 - 3 - 1) / 3;
  155.       this.buffer = ((Component)this).createImage(this.field_0, this.field_1);
  156.       this.field_2 = this.buffer.getGraphics();
  157.       this.playable = false;
  158.       this.mat = new int[3][3];
  159.       if (this.first == 0) {
  160.          this.askFirst();
  161.       } else if (this.first == 2) {
  162.          this.computerPlays();
  163.          ++this.nCoups;
  164.          this.playable = true;
  165.       } else {
  166.          this.playable = true;
  167.       }
  168.  
  169.       ((Component)this).repaint();
  170.    }
  171.  
  172.    public void getParameters() {
  173.       this.field_0 = ((Component)this).getSize().width;
  174.       this.field_1 = ((Component)this).getSize().height;
  175.       String s = ((Applet)this).getParameter("mode");
  176.       if (s != null) {
  177.          this.mode = Integer.parseInt(s);
  178.       }
  179.  
  180.       s = ((Applet)this).getParameter("first");
  181.       if (s != null) {
  182.          this.first = Integer.parseInt(s);
  183.       }
  184.  
  185.       this.backColor = this.getColor("background_color");
  186.       ((Component)this).setBackground(this.backColor);
  187.       this.borderColor = this.getColor("border_color");
  188.       s = ((Applet)this).getParameter("clic_sound_name");
  189.       if (s != null) {
  190.          this.clicSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  191.       }
  192.  
  193.       s = ((Applet)this).getParameter("computer_sound_name");
  194.       if (s != null) {
  195.          this.computerSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  196.       }
  197.  
  198.       s = ((Applet)this).getParameter("player_sound_name");
  199.       if (s != null) {
  200.          this.playerSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  201.       }
  202.  
  203.       s = ((Applet)this).getParameter("draw_sound_name");
  204.       if (s != null) {
  205.          this.drawSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  206.       }
  207.  
  208.    }
  209.  
  210.    public Color getColor(String param) {
  211.       String s = ((Applet)this).getParameter(param);
  212.       if (s != null) {
  213.          if (s.substring(0, 1).equals("#")) {
  214.             s = s.substring(1);
  215.             int i = Integer.parseInt(s, 16);
  216.             return new Color(i);
  217.          } else {
  218.             return null;
  219.          }
  220.       } else {
  221.          return null;
  222.       }
  223.    }
  224.  
  225.    public void askFirst() {
  226.       this.field_3 = new Panel();
  227.       this.field_3.setLayout(new GridLayout(2, 2));
  228.       Button b1 = new Button("player");
  229.       b1.addActionListener(this);
  230.       Button b2 = new Button("computer");
  231.       b2.addActionListener(this);
  232.       this.field_3.add(new Label("First"));
  233.       this.field_3.add(new Label("player ?"));
  234.       this.field_3.add(b1);
  235.       this.field_3.add(b2);
  236.       ((Container)this).add("Center", this.field_3);
  237.    }
  238.  
  239.    public void actionPerformed(ActionEvent event) {
  240.       if (((EventObject)event).getSource() instanceof Button) {
  241.          Button b = (Button)((EventObject)event).getSource();
  242.          ((Container)this).remove(this.field_3);
  243.          if (b.getLabel().equals("computer")) {
  244.             this.computerPlays();
  245.             ++this.nCoups;
  246.          }
  247.  
  248.          ((Component)this).repaint();
  249.          this.playable = true;
  250.       }
  251.  
  252.    }
  253.  
  254.    public final void paint(Graphics g) {
  255.       if (this.loaded) {
  256.          this.paintGrid(this.field_2);
  257.          this.paintCases(this.field_2);
  258.          if (this.textable) {
  259.             this.paintText(this.field_2);
  260.          }
  261.  
  262.          g.drawImage(this.buffer, 0, 0, this);
  263.       }
  264.    }
  265.  
  266.    public final void update(Graphics g) {
  267.       this.paint(g);
  268.    }
  269.  
  270.    public final void paintGrid(Graphics b) {
  271.       for(int i = 0; i <= 3; ++i) {
  272.          b.setColor(this.borderColor);
  273.          b.drawLine(i * (this.cote + 1), 0, i * (this.cote + 1), 3 * (this.cote + 1));
  274.          b.drawLine(0, i * (this.cote + 1), 3 * (this.cote + 1), i * (this.cote + 1));
  275.       }
  276.  
  277.    }
  278.  
  279.    public final void paintCases(Graphics b) {
  280.       for(int i = 1; i <= 3; ++i) {
  281.          for(int j = 1; j <= 3; ++j) {
  282.             if (this.mat[i - 1][j - 1] == 1) {
  283.                b.drawImage(this.crossIm, (j - 1) * (this.cote + 1) + 1, (i - 1) * (this.cote + 1) + 1, this.cote, this.cote, this);
  284.             } else if (this.mat[i - 1][j - 1] == 5) {
  285.                b.drawImage(this.circleIm, (j - 1) * (this.cote + 1) + 1, (i - 1) * (this.cote + 1) + 1, this.cote, this.cote, this);
  286.             }
  287.          }
  288.       }
  289.  
  290.    }
  291.  
  292.    public final void paintText(Graphics b) {
  293.       int h = this.cote / 3;
  294.       b.setFont(new Font("TimesRoman", 1, h));
  295.       b.setColor(this.borderColor);
  296.       FontMetrics fm = b.getFontMetrics();
  297.       int w = fm.stringWidth(this.text);
  298.       b.drawString(this.text, (this.field_0 - w) / 2, this.field_1 / 2);
  299.       this.textable = false;
  300.    }
  301.  
  302.    public void reset() {
  303.       for(int i = 1; i <= 3; ++i) {
  304.          for(int j = 1; j <= 3; ++j) {
  305.             this.mat[i - 1][j - 1] = 0;
  306.          }
  307.       }
  308.  
  309.       this.nCoups = 0;
  310.       this.mem = "000 000 000 000 000 000 000 000";
  311.       this.field_2.clearRect(0, 0, this.field_0, this.field_1);
  312.       ((Component)this).repaint();
  313.       if (this.first == 0) {
  314.          this.playable = false;
  315.          ((Container)this).add("Center", this.field_3);
  316.       }
  317.  
  318.    }
  319.  
  320.    public final boolean occupe(int i, int j) {
  321.       return !this.mem.substring(this.rep[i - 1][j - 1][0], this.rep[i - 1][j - 1][0] + 1).equals("0");
  322.    }
  323.  
  324.    public final void rempli(int i, int j, String s) {
  325.       for(int k = 0; k < this.rep[i - 1][j - 1].length; ++k) {
  326.          String s1 = this.mem.substring(0, this.rep[i - 1][j - 1][k]);
  327.          String s2 = this.mem.substring(this.rep[i - 1][j - 1][k] + 1);
  328.          this.mem = s1 + s + s2;
  329.          this.mat[i - 1][j - 1] = Integer.parseInt(s);
  330.       }
  331.  
  332.    }
  333.  
  334.    public final int[] donneIJ(int f) {
  335.       for(int i = 0; i < 3; ++i) {
  336.          for(int j = 0; j < 3; ++j) {
  337.             for(int k = 0; k < this.rep[i][j].length; ++k) {
  338.                if (this.rep[i][j][k] == f) {
  339.                   int[] ind = new int[]{i + 1, j + 1};
  340.                   return ind;
  341.                }
  342.             }
  343.          }
  344.       }
  345.  
  346.       return null;
  347.    }
  348.  
  349.    public final boolean resolution() {
  350.       if (this.mem.indexOf("111") != -1) {
  351.          if (this.playerSound != null) {
  352.             this.playerSound.play();
  353.          }
  354.  
  355.          this.text = "player wins !";
  356.          this.textable = true;
  357.          this.resetable = true;
  358.          return true;
  359.       } else if (this.mem.indexOf("555") != -1) {
  360.          if (this.computerSound != null) {
  361.             this.computerSound.play();
  362.          }
  363.  
  364.          this.text = "computer wins !";
  365.          this.textable = true;
  366.          this.resetable = true;
  367.          return true;
  368.       } else {
  369.          return false;
  370.       }
  371.    }
  372.  
  373.    public final void computerPlays() {
  374.       if (this.mode == 1) {
  375.          String[] cod = new String[]{"550", "055", "505", "110", "101", "011"};
  376.  
  377.          for(int i = 0; i < cod.length; ++i) {
  378.             int f = this.mem.indexOf(cod[i]);
  379.             if (f != -1) {
  380.                f = this.mem.indexOf("0", f);
  381.                int[] ind = this.donneIJ(f);
  382.                this.rempli(ind[0], ind[1], "5");
  383.                ((Component)this).repaint();
  384.                return;
  385.             }
  386.          }
  387.  
  388.          if (!this.occupe(2, 2)) {
  389.             this.rempli(2, 2, "5");
  390.             ((Component)this).repaint();
  391.             return;
  392.          }
  393.  
  394.          if (this.mat[1][1] == 1) {
  395.             if (!this.occupe(3, 1)) {
  396.                this.rempli(3, 1, "5");
  397.                ((Component)this).repaint();
  398.                return;
  399.             }
  400.  
  401.             if (!this.occupe(3, 3)) {
  402.                this.rempli(3, 3, "5");
  403.                ((Component)this).repaint();
  404.                return;
  405.             }
  406.  
  407.             if (!this.occupe(1, 1)) {
  408.                this.rempli(1, 1, "5");
  409.                ((Component)this).repaint();
  410.                return;
  411.             }
  412.          } else if (this.mat[1][1] == 5) {
  413.             if (this.occupe(3, 1) && !this.occupe(1, 3)) {
  414.                this.rempli(1, 3, "5");
  415.                ((Component)this).repaint();
  416.                return;
  417.             }
  418.  
  419.             if (this.occupe(3, 3) && !this.occupe(1, 1)) {
  420.                this.rempli(1, 1, "5");
  421.                ((Component)this).repaint();
  422.                return;
  423.             }
  424.  
  425.             if (this.occupe(1, 3) && !this.occupe(3, 1)) {
  426.                this.rempli(3, 1, "5");
  427.                ((Component)this).repaint();
  428.                return;
  429.             }
  430.  
  431.             if (this.occupe(1, 1) && !this.occupe(3, 3)) {
  432.                this.rempli(3, 3, "5");
  433.                ((Component)this).repaint();
  434.                return;
  435.             }
  436.  
  437.             if (this.occupe(3, 2) && !this.occupe(1, 3)) {
  438.                this.rempli(1, 3, "5");
  439.                ((Component)this).repaint();
  440.                return;
  441.             }
  442.  
  443.             if (this.occupe(1, 2) && !this.occupe(3, 3)) {
  444.                this.rempli(3, 3, "5");
  445.                ((Component)this).repaint();
  446.                return;
  447.             }
  448.  
  449.             if (this.occupe(2, 1) && !this.occupe(1, 3)) {
  450.                this.rempli(1, 3, "5");
  451.                ((Component)this).repaint();
  452.                return;
  453.             }
  454.  
  455.             if (this.occupe(2, 3) && !this.occupe(3, 1)) {
  456.                this.rempli(3, 1, "5");
  457.                ((Component)this).repaint();
  458.                return;
  459.             }
  460.          }
  461.  
  462.          String[] cod2 = new String[]{"005", "050", "500"};
  463.  
  464.          for(int i = 0; i < cod2.length; ++i) {
  465.             int f = this.mem.indexOf(cod2[i]);
  466.             if (f != -1) {
  467.                f = this.mem.indexOf("0", f);
  468.                int[] ind = this.donneIJ(f);
  469.                this.rempli(ind[0], ind[1], "5");
  470.                ((Component)this).repaint();
  471.                return;
  472.             }
  473.          }
  474.       }
  475.  
  476.       int i;
  477.       int j;
  478.       do {
  479.          int r = (int)((double)9.0F * Math.random());
  480.          i = r / 3 + 1;
  481.          j = r % 3 + 1;
  482.       } while(this.occupe(i, j));
  483.  
  484.       this.rempli(i, j, "5");
  485.       ((Component)this).repaint();
  486.    }
  487.  
  488.    public void draw() {
  489.       this.text = "draw !";
  490.       this.textable = true;
  491.       ((Component)this).repaint();
  492.       this.resetable = true;
  493.    }
  494.  
  495.    public void mouseClicked(MouseEvent e) {
  496.    }
  497.  
  498.    public void mouseEntered(MouseEvent e) {
  499.    }
  500.  
  501.    public void mouseExited(MouseEvent e) {
  502.    }
  503.  
  504.    public void mousePressed(MouseEvent e) {
  505.       if (this.playable) {
  506.          if (this.resetable) {
  507.             this.reset();
  508.             this.resetable = false;
  509.          } else if (((InputEvent)e).isMetaDown()) {
  510.             this.reset();
  511.          } else {
  512.             int x = e.getX();
  513.             int y = e.getY();
  514.             int j = (x - 1) / (this.cote + 1) + 1;
  515.             int i = (y - 1) / (this.cote + 1) + 1;
  516.             if (!this.occupe(i, j)) {
  517.                if (this.clicSound != null) {
  518.                   this.clicSound.play();
  519.                }
  520.  
  521.                this.rempli(i, j, "1");
  522.                ((Component)this).repaint();
  523.                if (this.resolution()) {
  524.                   return;
  525.                }
  526.  
  527.                ++this.nCoups;
  528.                if (this.nCoups == 9) {
  529.                   this.draw();
  530.                   if (this.drawSound != null) {
  531.                      this.drawSound.play();
  532.                   }
  533.  
  534.                   return;
  535.                }
  536.  
  537.                this.computerPlays();
  538.                if (this.resolution()) {
  539.                   return;
  540.                }
  541.  
  542.                ++this.nCoups;
  543.                if (this.nCoups == 9) {
  544.                   this.draw();
  545.                   if (this.drawSound != null) {
  546.                      this.drawSound.play();
  547.                   }
  548.  
  549.                   return;
  550.                }
  551.             }
  552.  
  553.          }
  554.       }
  555.    }
  556.  
  557.    public void mouseReleased(MouseEvent e) {
  558.    }
  559. }
  560.